﻿Imports Negocio
Imports Servicios
Imports Excepciones

'TODO: Uso de Hilos
Public Class frmConsultaRecurso

    Private _recursosCargados As New List(Of Recurso)

    Sub New()
        InitializeComponent()
    End Sub

    Sub New(recursos As List(Of Recurso))
        Me.New()
        _recursosCargados = recursos
        btnAgregar.Visible = True
    End Sub

    Protected Overrides Sub PreLoad()
        dgvRecursos.AutoGenerateColumns = False
        Me.Icon = Icon.FromHandle(Global.SIPRO.My.Resources.Resources.Person.GetHicon())
    End Sub

    Private Sub frmConsultaBitacora_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Init()
    End Sub

    Sub Init()
        DisplayPleaseWait()
        BGWSearch.RunWorkerAsync(New Object() {txtDNI.Text, txtNOMBRE.Text, txtAPELLIDO.Text})

    End Sub


    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        If MyBase.ValidateForm() Then
            Init()
        End If
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCerrar.Click
        Me.Close()
    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click
        If dgvRecursos.Rows.Count > 0 AndAlso TypeOf dgvRecursos.CurrentRow.DataBoundItem Is Recurso Then
            If MessageExt.Show(GesIdiomas.TraduceMensaje("MSGRE001").ToString, MessageBoxButtons.OKCancel) = MsgBoxResult.Ok Then
                Dim rec As Recurso = DirectCast(dgvRecursos.CurrentRow.DataBoundItem, Recurso)
                Try
                    GesRecursos.Eliminar(rec)
                Catch ex As Exception
                    My.Application.HandlerException(ex)
                End Try
                Init()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Dim f = New frmABMRecurso
        If f.ShowDialog = DialogResult.OK Then
            Init()
        End If
    End Sub

    Private Sub dgvRecursos_CellMouseDoubleClick(sender As System.Object, e As System.EventArgs) Handles dgvRecursos.DoubleClick
        If dgvRecursos.Rows.Count > 0 AndAlso TypeOf dgvRecursos.CurrentRow.DataBoundItem Is Recurso Then
            Dim rec As Recurso = DirectCast(dgvRecursos.CurrentRow.DataBoundItem, Recurso)
            Dim f As New frmABMRecurso(rec)
            If f.ShowDialog = DialogResult.OK Then
                Init()
            End If
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If
    End Sub

    Private Sub btnAgregar_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregar.Click

        If dgvRecursos.Rows.Count > 0 AndAlso TypeOf dgvRecursos.CurrentRow.DataBoundItem Is Recurso Then

            Dim rec As Recurso = DirectCast(dgvRecursos.CurrentRow.DataBoundItem, Recurso)
            Try
                rec.Persistencia = EstadoPersistencia.INSERT
                _recursosCargados.Add(rec)
                Me.Close()
                Me.DialogResult = Windows.Forms.DialogResult.OK
            Catch ex As Exception
                My.Application.HandlerException(ex)
            End Try
        Else
            My.Application.HandlerException(New ItemSelectRequiredException())
        End If

    End Sub

    Private Sub BGWSearch_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGWSearch.DoWork

        System.Threading.Thread.Sleep(2000)

        Dim args As Object() = DirectCast(e.Argument, Object())

        Dim dni As String = CStr(args(0))
        Dim nombre As String = CStr(args(1))
        Dim apellido As String = CStr(args(2))

        Dim recursos As List(Of Recurso) = GesRecursos.Consultar(dni, nombre, apellido)

        e.Result = recursos

    End Sub

    Private Sub BGWSearch_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWSearch.RunWorkerCompleted

        Try
            Dim recursos As List(Of Recurso) = DirectCast(e.Result, List(Of Recurso))
            'GesRecursos.Consultar(txtDNI.Text, txtNOMBRE.Text, txtAPELLIDO.Text)

            For Each rec As Recurso In _recursosCargados
                If recursos.Contains(rec) Then
                    recursos.Remove(rec)
                End If
            Next
            dgvRecursos.DataSource = recursos
            HidePleaseWait()
        Catch ex As Exception
            My.Application.HandlerException(ex)
            Me.Close()
        End Try

    End Sub

    Private Sub DisplayPleaseWait()
        btnBuscar.Enabled = False
        Dim picBox As PictureBox
        picBox = New PictureBox
        picBox.Name = "picBoxWait"
        picBox.Image = My.Resources.wait

        picBox.Anchor = dgvRecursos.Anchor
        picBox.Size = dgvRecursos.Size
        picBox.Location = dgvRecursos.Location

        picBox.BackColor = Color.FromArgb(100, 255, 255, 255)
        picBox.SizeMode = PictureBoxSizeMode.CenterImage
        picBox.Refresh()
        picBox.Enabled = True
        Me.Controls.Add(picBox)
        picBox.BringToFront()


    End Sub

    Private Sub HidePleaseWait()
        Try
            Dim ctrl As System.Windows.Forms.Control = Me.Controls.Find("picBoxWait", True)(0)
            Me.Controls.Remove(ctrl)
        Catch ex As Exception

        Finally
            btnBuscar.Enabled = True
        End Try

    End Sub

End Class